﻿using LightCAD.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QdArch.Shapes
{
    /// <summary>
    /// 平开门
    /// </summary>
    public class SwingDoor : IShape2dProvider
    {
        public ComponentShape2d[] GetShapes(string category)
        {
            var subCategory = DoorCategory.SwingDoor.ToString();
            var list = new List<ComponentShape2d>
            {
                CreateShape(subCategory,"Single45", nameof(GetShape_单扇平开门_半开), GetShape_单扇平开门_半开, ""),
                CreateShape(subCategory,"Single60", nameof(GetShape_单扇平开门_60度), GetShape_单扇平开门_60度, ""),
                CreateShape(subCategory,"Single90", nameof(GetShape_单扇平开门_全开), GetShape_单扇平开门_全开, ""),
                CreateShape(subCategory,"Double45", nameof(GetShape_双扇平开门_半开), GetShape_双扇平开门_半开, ""),
                CreateShape(subCategory,"Double60", nameof(GetShape_双扇平开门_60度), GetShape_双扇平开门_60度, ""),
                CreateShape(subCategory,"Double90", nameof(GetShape_双扇平开门_全开), GetShape_双扇平开门_全开, "")
           };
            return list.ToArray();
        }

        public bool IsSupportCategory(string category)
        {
            return (category == ArchCategory.Door.ToString());
        }
        public ComponentShape2d CreateShape(string subCategory, string name, string funcName, Func<LcParameterSet, List<ICurve2d>> func, string description)
        {
            var displayName = funcName.Substring(funcName.IndexOf("_") + 1);
            return new ComponentShape2d
            {
                Category = ArchCategory.Door.ToString(),
                SubCategory = subCategory,
                Name = name,
                DisplayName = displayName,
                Description = description,
                Generate = func
            };
        }
        public List<ICurve2d> GetShape_单扇平开门_半开(LcParameterSet pset)
        {
            var sc = new Shape2dCreator();

            var w = (double)pset["Width"];
            //var th = (double)pset["Thickness"];
            var isLeft = (bool)pset["IsLeft"];
            var isNear = (bool)pset["IsNear"];

            var pL = sc.Point(-w / 2, 0);
            var pR = sc.Point(w / 2, 0);
            var pLNear = sc.RotatePoint(pL, pR, 45);
            var pLFar = sc.RotatePoint(pL, pR, -45);
            var pRNear = sc.RotatePoint(pR, pL, -45);
            var pRFar = sc.RotatePoint(pR, pL, 45);
            if (isLeft && isNear)
            {
                sc.DrawArc(pL, w, -45, 0);
                sc.DrawLine(pL, pRNear);
            }
            else if (isLeft && !isNear)
            {
                sc.DrawArc(pL, w, 0, +45);
                sc.DrawLine(pL, pRFar);
            }
            else if (!isLeft && isNear)
            {
                sc.DrawArc(pR, w, 180, 180 + 45);
                sc.DrawLine(pR, pLNear);
            }
            else if (!isLeft && !isNear)
            {
                sc.DrawArc(pR, w, 180 - 45, 180);
                sc.DrawLine(pR, pLFar);
            }

            return sc.Curves;
        }

        public List<ICurve2d> GetShape_单扇平开门_60度(LcParameterSet pset)
        {
            var sc = new Shape2dCreator();

            var w = (double)pset["Width"];
            //var th = (double)pset["Thickness"];
            var isLeft = (bool)pset["IsLeft"];
            var isNear = (bool)pset["IsNear"];

            var pL = sc.Point(-w / 2, 0);
            var pR = sc.Point(w / 2, 0);
            var pLNear = sc.RotatePoint(pL, pR, 60);
            var pLFar = sc.RotatePoint(pL, pR, -60);
            var pRNear = sc.RotatePoint(pR, pL, -60);
            var pRFar = sc.RotatePoint(pR, pL, 60);
            if (isLeft && isNear)
            {
                sc.DrawArc(pL, w, -60, 0);
                sc.DrawLine(pL, pRNear);
            }
            else if (isLeft && !isNear)
            {
                sc.DrawArc(pL, w, 0, +60);
                sc.DrawLine(pL, pRFar);
            }
            else if (!isLeft && isNear)
            {
                sc.DrawArc(pR, w, 180, 180 + 60);
                sc.DrawLine(pR, pLNear);
            }
            else if (!isLeft && !isNear)
            {
                sc.DrawArc(pR, w, 180 - 60, 180);
                sc.DrawLine(pR, pLFar);
            }

            return sc.Curves;
        }

        public List<ICurve2d> GetShape_单扇平开门_全开(LcParameterSet pset)
        {
            var sc = new Shape2dCreator();

            var w = (double)pset["Width"];
            //var th = (double)pset["Thickness"];
            var isLeft = (bool)pset["IsLeft"];
            var isNear = (bool)pset["IsNear"];

            var pL = sc.Point(-w / 2, 0);
            var pR = sc.Point(w / 2, 0);
            var pLNear = sc.RotatePoint(pL, pR, 90);
            var pLFar = sc.RotatePoint(pL, pR, -90);
            var pRNear = sc.RotatePoint(pR, pL, -90);
            var pRFar = sc.RotatePoint(pR, pL, 90);
            if (isLeft && isNear)
            {
                sc.DrawArc(pL, w, -90, 0);
                sc.DrawLine(pL, pRNear);
            }
            else if (isLeft && !isNear)
            {
                sc.DrawArc(pL, w, 0, +90);
                sc.DrawLine(pL, pRFar);
            }
            else if (!isLeft && isNear)
            {
                sc.DrawArc(pR, w, 180, 180 + 90);
                sc.DrawLine(pR, pLNear);
            }
            else if (!isLeft && !isNear)
            {
                sc.DrawArc(pR, w, 180 - 90, 180);
                sc.DrawLine(pR, pLFar);
            }

            return sc.Curves;
        }

        public List<ICurve2d> GetShape_双扇平开门_半开(LcParameterSet pset)
        {
            var sc = new Shape2dCreator();

            var w = (double)pset["Width"];
            //var th = (double)pset["Thickness"];
            var isLeft = (bool)pset["IsLeft"];
            var isNear = (bool)pset["IsNear"];

            var pC = sc.Point(0, 0);
            var pL = sc.Point(-w / 2, 0);
            var pR = sc.Point(w / 2, 0);
            var pCLNear = sc.RotatePoint(pC, pL, -45);
            var pCLFar = sc.RotatePoint(pC, pL, 45);
            var pCRNear = sc.RotatePoint(pC, pR, 45);
            var pCRFar = sc.RotatePoint(pC, pR, -45);
            if (isNear)
            {
                sc.DrawArc(pL, w / 2, -45, 0);
                sc.DrawLine(pL, pCLNear);
                sc.DrawArc(pR, w / 2, 180, 180 + 45);
                sc.DrawLine(pR, pCRNear);
            }
            else
            {
                sc.DrawArc(pL, w / 2, 0, 45);
                sc.DrawLine(pL, pCLFar);
                sc.DrawArc(pR, w / 2, 180 - 45, 180);
                sc.DrawLine(pR, pCRFar);
            }

            return sc.Curves;
        }

        public List<ICurve2d> GetShape_双扇平开门_60度(LcParameterSet pset)
        {
            var sc = new Shape2dCreator();

            var w = (double)pset["Width"];
            //var th = (double)pset["Thickness"];
            var isLeft = (bool)pset["IsLeft"];
            var isNear = (bool)pset["IsNear"];

            var pC = sc.Point(0, 0);
            var pL = sc.Point(-w / 2, 0);
            var pR = sc.Point(w / 2, 0);
            var pCLNear = sc.RotatePoint(pC, pL, -60);
            var pCLFar = sc.RotatePoint(pC, pL, 60);
            var pCRNear = sc.RotatePoint(pC, pR, 60);
            var pCRFar = sc.RotatePoint(pC, pR, -60);
            if (isNear)
            {
                sc.DrawArc(pL, w / 2, -60, 0);
                sc.DrawLine(pL, pCLNear);
                sc.DrawArc(pR, w / 2, 180, 180 + 60);
                sc.DrawLine(pR, pCRNear);
            }
            else
            {
                sc.DrawArc(pL, w / 2, 0, 60);
                sc.DrawLine(pL, pCLFar);
                sc.DrawArc(pR, w / 2, 180 - 60, 180);
                sc.DrawLine(pR, pCRFar);
            }

            return sc.Curves;
        }

        public List<ICurve2d> GetShape_双扇平开门_全开(LcParameterSet pset)
        {
            var sc = new Shape2dCreator();

            var w = (double)pset["Width"];
            //var th = (double)pset["Thickness"];
            var isLeft = (bool)pset["IsLeft"];
            var isNear = (bool)pset["IsNear"];

            var pC = sc.Point(0, 0);
            var pL = sc.Point(-w / 2, 0);
            var pR = sc.Point(w / 2, 0);
            var pCLNear = sc.RotatePoint(pC, pL, -90);
            var pCLFar = sc.RotatePoint(pC, pL, 90);
            var pCRNear = sc.RotatePoint(pC, pR, 90);
            var pCRFar = sc.RotatePoint(pC, pR, -90);
            if (isNear)
            {
                sc.DrawArc(pL, w / 2, -90, 0);
                sc.DrawLine(pL, pCLNear);
                sc.DrawArc(pR, w / 2, 180, 180 + 90);
                sc.DrawLine(pR, pCRNear);
            }
            else
            {
                sc.DrawArc(pL, w / 2, 0, 90);
                sc.DrawLine(pL, pCLFar);
                sc.DrawArc(pR, w / 2, 180 - 90, 180);
                sc.DrawLine(pR, pCRFar);
            }

            return sc.Curves;
        }
    }
}
